package com.phkj.qdh.ui.fragment.usecar

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.hjq.toast.ToastUtils
import com.phkj.qdh.base.BaseVM
import com.phkj.qdh.bean.ApprovalManBean
import com.phkj.qdh.bean.CarKeyBean
import com.phkj.qdh.bean.UseCarBean
import com.phkj.qdh.bean.UseCarMyBean
import com.phkj.qdh.bean.UseCarMyDetail
import com.phkj.qdh.net.code
import com.phkj.qdh.net.msg
import com.phkj.qdh.repository.CarRepository
import com.phkj.qdh.utils.requestIO
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.onCompletion

class CarVM(val carRepository: CarRepository) : BaseVM() {

    private val _hasPermission = MutableSharedFlow<Boolean>()
    val hasPermission get() = _hasPermission
    fun hasPermission() {
        requestIO {
            carRepository.hasPermission().catch { cause ->
                ToastUtils.show(cause.msg)
            }.collectLatest {
                _hasPermission.emit(it)
            }
        }
    }

    //申请列表(自己）
    private val _useCarMyList = MutableSharedFlow<ArrayList<UseCarMyBean>>()
    val useCarMyList get() = _useCarMyList
    fun getUseCarMyList(map: HashMap<String,Any?>){
        requestIO {
            carRepository.useCarListMy(map)
                .catch {case->
                    ToastUtils.show(case.msg)
                    loadFail()
                }.onCompletion {
                    stopLoading()
                }
                .collectLatest {
                    _useCarMyList.emit(it.data)
                    loadSuccess()
                }
        }
    }

    //申请列表(自己）
    private val _useCarMyDetail = MutableSharedFlow<UseCarMyDetail>()
    val useCarMyDetail get() = _useCarMyDetail
    fun getUseCarMyDetail(map: HashMap<String,Any?>){
        requestIO {
            carRepository.useCarDetailMy(map)
                .catch {case->
                    ToastUtils.show(case.msg)
                    loadFail()
                }.onCompletion {
                    stopLoading()
                }
                .collectLatest {
                    _useCarMyDetail.emit(it)
                    loadSuccess()
                }
        }
    }


    //申请列表(审批人）
    private val _useCarList = MutableSharedFlow<ArrayList<UseCarBean>>()
    val useCarList get() = _useCarList
    fun getUseCarList(map: HashMap<String,Any?>){
        requestIO {
            carRepository.useCarList(map)
                .catch {case->
                    ToastUtils.show(case.msg)
                    loadFail()
                }.onCompletion {
                    stopLoading()
                }
                .collectLatest {
                    _useCarList.emit(it.data)
                    loadSuccess()
                }
        }
    }

    //车辆要是列表
    private val _carKey = MutableSharedFlow<ArrayList<CarKeyBean>>()
    val carKey get() = _carKey
    fun getCarKeyList(map: HashMap<String,Any?>){
        requestIO {
            carRepository.carKeyList(map)
                .catch {case->
                    ToastUtils.show(case.msg)
                }
                .collectLatest {
                    _carKey.emit(it)
                }
        }
    }


    //车辆要是列表
    private val _approvalManBean = MutableSharedFlow<ArrayList<ApprovalManBean>>()
    val approvalManBean get() = _approvalManBean
    fun getApprovalManList(){
        requestIO {
            carRepository.getApprovalManList()
                .catch {case->
                    ToastUtils.show(case.msg)
                }
                .collectLatest {
                    _approvalManBean.emit(it.data)
                }
        }
    }


    //用车申请提交
    private val _carUseSubmit = MutableSharedFlow<Int>()
    val carUseSubmit get() = _carUseSubmit
    fun carUseSubmit(map:HashMap<String,Any?>){
        requestIO {
            carRepository.useCarSubmit(map)
                .catch {case->
                    _carUseSubmit.emit(case.code)
                }
                .collectLatest {
//                    _carUseSubmit.emit(it)
                }
        }
    }


    //用车申请审批
    private val _carUseApproval = MutableSharedFlow<Int>()
    val carUseApproval get() = _carUseApproval
    fun carUseApproval(map:HashMap<String,Any?>){
        requestIO {
            carRepository.carUseApproval(map)
                .catch {case->
                    _carUseApproval.emit(case.code)
                }
                .collectLatest {
//                    _carUseSubmit.emit(it)
                }
        }
    }


}


fun getCarVMProvider() = CarVMProvider(CarRepository)

class CarVMProvider(val carRepository: CarRepository) : ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        return CarVM(carRepository) as T
    }
}